package ConnectionsCommands;

import Gear.Factory;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implement received command to lock access of an especified file.
 *
 * @author Rodrigo Martins Racanicci
 */
public class ConnectionCommandLockFile implements ConnectionCommand {

    @Override
    public void execute(Socket client, String[] args) {
        try {
            Factory.getInstance().getTracker().getFileMutex().acquire();

            Factory.getInstance().getTracker().sendCommand(client, Boolean.toString(Factory.getInstance().getTracker().lockFile(client.getInetAddress().toString(), args[1])));
            Factory.getInstance().getTracker().closeClient(client);

            Factory.getInstance().getTracker().getFileMutex().release();
        } catch (InterruptedException ex) {
            Logger.getLogger(ConnectionCommandLockFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
